//
//  AddressViewController.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "AddressViewController.h"
#import "AddFirendVCOfFVQM.h"

#import "MesseageVCOfFVQM.h"
#import "MessageSendViewController.h"



@interface AddressViewController ()<UITableViewDataSource,UITableViewDelegate,EMChatManagerDelegate,EMChatManagerBuddyDelegate>
@property(nonatomic, strong) NSArray *boddies;
@property(nonatomic, strong) UITableView *tableView;


@end

@implementation AddressViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    
}


-(void)loadView{
    [super loadView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    

    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"tianjialianxiren"] style:UIBarButtonItemStylePlain target:self action:@selector(addbuttonAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.boddies = [NSArray array];
    //本地获取好友列表
   self.boddies =  [[EaseMob sharedInstance].chatManager buddyList];
    if (self.boddies.count == 0) {
        
        //从服务器上获取好友列表
        [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
            
            self.boddies = buddyList;
            [self.tableView reloadData];
        } onQueue:nil];
    }
    
    
}

//跳转到添加好友界面

-(void)addbuttonAction{
    [self.navigationController pushViewController:[[AddFirendVCOfFVQM alloc]init] animated:YES];
}


# pragma mark - Table View Data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.boddies.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    
    EMBuddy * buddy = self.boddies[indexPath.row];
    
    cell.textLabel.text = buddy.username;
    cell.imageView.image = [UIImage imageNamed:@"Icon"];
    
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    return UITableViewCellEditingStyleDelete;
}


 /**删除好友的方法，从列表移除*/

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EMBuddy *buddy = self.boddies[indexPath.row];
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
              
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否删除好友" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
           [[EaseMob sharedInstance].chatManager removeBuddy:buddy.username removeFromRemote:YES error:nil];
            
            [self.tableView reloadData];
        
        }];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        
    }
    
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.hidesBottomBarWhenPushed = YES;
   MessageSendViewController * message = [[MessageSendViewController   alloc]init];
    
   // message.buddy = self.boddies[indexPath.row];
    
   // [message setHidesBottomBarWhenPushed:YES];
    
    message.buddy = [_boddies objectAtIndex:indexPath.section];

    [self.navigationController pushViewController:message animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}



// 更新好友列表
-(void)didUpdateBuddyList:(NSArray *)buddyList changedBuddies:(NSArray *)changedBuddies isAdd:(BOOL)isAdd{
    
    NSString *str = isAdd ?@"添加的":@"删除的";
    NSLog(@"添加或者删除 = %@", str);
    
    self.boddies = buddyList;
//    [self.tableView reloadData];
    
    
    
}

//好友同意添加的调用
-(void)didAcceptBuddySucceed:(NSString *)username{
    
    NSLog(@"同意添加好友成功 = %@",username);
    
    
    
    
    
}

//当前用户被别人移除调用
-(void)didRemovedByBuddy:(NSString *)username{
    
    NSLog(@"我被%@删除",username);
    
    
    
}

@end
